package com.nulldev.util.web.HttpServer3.extensions.envStats;

import java.util.Map;

import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.manage.nullUtil;
import com.nulldev.util.manage.experiments.Experiment;
import com.nulldev.util.manage.experiments.Experiments;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;

public class ExperimentStatsPage extends HttpPage3 {

	private static final String JSON_MIME = "application/json; charset=utf-8";

	public ExperimentStatsPage() {
		super("/debug/experiments");
	}

	@SuppressWarnings("resource")
	@Override
	public Response onRequest(final Request request) {
		if (!nullUtil.IS_NON_MAINLINE_BUILD)
			return new Response(204, BLANK_IO_STREAM);
		final UniJsonValue uv = UniJsonValue.array();
		for (final Map.Entry<String, Experiment> EXPERIMENT : Experiments.getAllExperiments().entrySet()) {
			final UniJsonValue entry = UniJsonValue.object(4);
			entry.add("key", EXPERIMENT.getKey());
			final Experiment exp = EXPERIMENT.getValue();
			entry.add("active", exp.isActive());
			entry.add("description", exp.description());
			entry.add("codeName", exp.codeName());
			uv.add(entry);
		}
		return new Response(200, uv, uv).setCustomMime(JSON_MIME);
	}

}
